package org.oschina.queue;

import org.oschina.helper.JsonHelper;
import org.oschina.helper.cache.ICacheHelper;
import org.oschina.helper.messagequeue.IMessageQueueHelper;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseQueue {

	@Autowired
	private IMessageQueueHelper messageQueueHelper;
	@Autowired
	private ICacheHelper cacheHelper;
	
	/**
	 * 获得某个队列的值
	 * @param key
	 * @param type
	 * @return
	 */
	protected <T> Object get(String key,Class<T> type)
	{
		return messageQueueHelper.get(key,type);
	}
	
	/**
	 * 判断是否应该更新
	 * @param time
	 * @return
	 */
	protected boolean isUpdate(int time)
	{
		String key="QUEUE_TIME_STAMP_"+this.getClass().getCanonicalName();
		if(cacheHelper.get(key,Boolean.class)==null)
		{
			cacheHelper.set(key, true, time);
			return true;
		}
		return false;
	}
	
	/**
	 * 解析json
	 * @param obj
	 * @param type
	 * @return
	 */
	protected Object parse(String json,Class type)
	{
		return JsonHelper.parse(json, type);
	}
	
	protected ICacheHelper getCacheHelper()
	{
		return cacheHelper;
	}
	
	protected IMessageQueueHelper getMessageQueueHelper()
	{
		return messageQueueHelper;
	}
	
}